package com.china08.yxyapi.api.manage.tec.teaching.lecture.prepare;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack;
import com.china08.yxyapi.entity.mg.lecture.YxyLectureRes;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePrePackRepository;
import com.china08.yxyapi.repository.mg.lecture.YxyLectureResRepository;

import io.swagger.annotations.ApiOperation;

/**
 * 老师 ---备授课--修改成品备课包
 * 
 */
@CrossOrigin
@RestController
public class LectureUptPrePackController extends YxyApiBaseController {

    @Autowired
    private YxyLecturePrePackRepository yxyLecturePrePackRepository;

    @Autowired
    private YxyLectureResRepository yxyLectureResRepository;

    @ApiOperation(value = "老师 ---备授课--修改成品备课包")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/prepare/upt", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel req,
            RespModel respModel) {

        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();
        List<Res> resp = req.getResp();

        YxyLecturePrePack yxyLecturePrePack = yxyLecturePrePackRepository
                .findByIdAndUserIdAndSchoolIdAndIsDeleteNot(req.id, userId, schoolId, 1);
        if (yxyLecturePrePack != null) {
            if (req.resp != null && !req.resp.isEmpty()) {
                List<com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack.Res> prePack = yxyLecturePrePack
                        .getResources();
                List<com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack.Res> resList1 = new ArrayList<>();
                yxyLecturePrePack.setResources(resList1);
                int max = 0;
                List<com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack.Res> resList = new ArrayList<>();
                for (Res res : resp) {
                    YxyLectureRes resourceId = yxyLectureResRepository.findTop1ByResourceId(res.getResourceId());
                    com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack.Res s = new YxyLecturePrePack().new Res();
                    s.setCourseType(res.getCourseType());
                    s.setFormatId(res.getFormatId());
                    s.setResourceName(res.getResourceName());
                    s.setResourceId(res.getResourceId());
                    s.setResourceUrl(res.getResourceUrl());
                    s.setOrderNum(max);
                    s.setFileSize(resourceId == null ? 0 : resourceId.getFileSize());
                    max++;
                    resList.add(s);
                }
                int mm = resList.size();
                for (com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack.Res res : prePack) {
                    YxyLectureRes resourceId = yxyLectureResRepository.findTop1ByResourceId(res.getResourceId());
                    com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack.Res s = new YxyLecturePrePack().new Res();
                    s.setCourseType(res.getCourseType());
                    s.setFormatId(res.getFormatId());
                    s.setResourceName(res.getResourceName());
                    s.setResourceId(res.getResourceId());
                    s.setResourceUrl(res.getResourceUrl());
                    s.setOrderNum(mm);
                    s.setFileSize(resourceId == null ? 0 : resourceId.getFileSize());
                    mm++;
                    resList.add(s);
                }
                yxyLecturePrePack.getResources().addAll(resList);
                yxyLecturePrePackRepository.save(yxyLecturePrePack);
            }
        }

        Assert.notNull(StringUtils.defaultIfBlank(userId, null), "用户id必须");
        Assert.notNull(StringUtils.defaultIfBlank(schoolId, null), "学校id必须");

        respModel = new RespModel();
        respModel.setMessage("成品备课包更新成功");
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

        public String id;

        /**
         * 资源id
         */
        public List<Res> resp;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(id, null), "备课包id不能为空");
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public List<Res> getResp() {
            return resp;
        }

        public void setResp(List<Res> resp) {
            this.resp = resp;
        }

    }

    protected static class Res {
        public String resourceId;
        public String resourceName;
        public String resourceUrl;
        public String formatId;
        // 0 试题 1资源
        public int courseType = 1;

        public String getResourceId() {
            return resourceId;
        }

        public void setResourceId(String resourceId) {
            this.resourceId = resourceId;
        }

        public String getResourceName() {
            return resourceName;
        }

        public void setResourceName(String resourceName) {
            this.resourceName = resourceName;
        }

        public String getResourceUrl() {
            return resourceUrl;
        }

        public void setResourceUrl(String resourceUrl) {
            this.resourceUrl = resourceUrl;
        }

        public String getFormatId() {
            return formatId;
        }

        public void setFormatId(String formatId) {
            this.formatId = formatId;
        }

        public int getCourseType() {
            return courseType;
        }

        public void setCourseType(int courseType) {
            this.courseType = courseType;
        }

    }

    protected static class RespModel extends YxyApiRespModel {

    }
}
